"""
    重构requests中的get/post方法
"""
import requests


class Request(object):

    def requests_api(self, url, data=None, json=None, headers=None, cookies=None, method="get", **kwargs):
        if method == "get":
            res = requests.get(url=url, data=data, json=json, headers=headers, cookies=cookies)
        elif method == "post":
            res = requests.post(url=url, data=data, json=json, headers=headers, cookies=cookies)

        # 获取内容结果
        code = res.status_code
        try:
            body = res.json()
        except Exception as e:
            body = res.text
        # 内容存到字典
        d = dict()
        d["code"] = code
        d["body"] = body

        return res

    def get(self, url, **kwargs):
        return self.requests_api(url, method="get", **kwargs)

    def post(self, url, **kwargs):
        return self.requests_api(url, method="post", **kwargs)